﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UWP.Douban.Models;
using UWP.Douban.Utils;

namespace UWP.Douban.Api
{
    public  class GroupApi:BaseApi
    {
        private static readonly string AddComment = "/v2/group/topic/{0}/add_comment";

        private static readonly string UserUrl = "/v2/group/user/{0}/";
        private const string AllGroupsUrl = "/v2/group/channels/all/groups";
        private const string CultureGroupsUrl = "/v2/group/channels/culture/topics";
        private const string TravelGroupsUrl = "/v2/group/channels/travel/topics";
        private const string EntGroupsUrl = "/v2/group/channels/ent/topics";
        private const string FashionGroupsUrl = "/v2/group/channels/fashion/topics";
        private const string LifeGroupsUrl = "/v2/group/channels/life/topics";
        private const string TechGroupsUrl = "/v2/group/channels/tech/topics";

        private const string AllTopicsUrl = "/v2/group/channels/all/topics";
        private const string CultureTopicsUrl = "/v2/group/channels/culture/topics";
        private const string TravelTopicsUrl = "/v2/group/channels/travel/topics";
        private const string EntTopicsUrl = "/v2/group/channels/ent/topics";

       

        private const string FashionTopicsUrl = "/v2/group/channels/fashion/topics";
        private const string LifeTopicsUrl = "/v2/group/channels/life/topics";
        private const string TechTopicsUrl = "/v2/group/channels/tech/topics";
        private const string MyTopicsUrl = "/v2/group/user_topics";

        private const string TopicCommentsUrl = "/v2/group/topic/{0}/comments";

        private const string AuthUrl = "https://www.douban.com:443/service/auth2/token";



        public static async Task<UserInfo> UserInfoGet(string userId)
        {
            var queryData = new[]
            {
                new KeyValuePair<string, string>("fields","rec_topic_count,like_topic_count,topic_count,reply_topic_count,album_count,join_group_count,user,join_groups"),
            };

            var url = BuildUri(UserUrl, userId);

            var json = await Search(url, queryData);

            return JsonUtil.Deserialize<UserInfo>(json);
        }


        public static async Task<IEnumerable<Topic>> GpSearchExploreTopics(int start = 0, int count = 20)
        {
            return await GpSearchTopics(AllTopicsUrl, start, count);
        }

        public static async Task<IEnumerable<Topic>> GpSearchCultureTopics(int start = 0, int count = 20)
        {
            return await GpSearchTopics(CultureTopicsUrl, start, count);
        }

        public static async Task<IEnumerable<Topic>> GpSearchTravelTopics(int start = 0, int count = 20)
        {
            return await GpSearchTopics(TravelTopicsUrl, start, count);
        }

        public static async Task<IEnumerable<Topic>> GpSearchEntTopics(int start = 0, int count = 20)
        {
            return await GpSearchTopics(EntTopicsUrl, start, count);
        }

        public static async Task<IEnumerable<Topic>> GpSearchFashionTopics(int start = 0, int count = 20)
        {
            return await GpSearchTopics(FashionTopicsUrl, start, count);
        }

        public static async Task<IEnumerable<Topic>> GpSearchLifeTopics(int start = 0, int count = 20)
        {
            return await GpSearchTopics(LifeTopicsUrl, start, count);
        }

        public static async Task<IEnumerable<Topic>> GpSearchTechTopics(int start = 0, int count = 20)
        {
            return await GpSearchTopics(TechTopicsUrl, start, count);
        }

        public static async Task<IEnumerable<Topic>> GpsearchMyTopics(int start = 0, int count = 20)
        {
            var url = BuildUri(MyTopicsUrl);

            return JsonUtil.DeseriEnumerable<Topic>(await Search(url, bearer: true, start: start, count: count),
                "$.topics[*]");
        }


        public static async Task<IEnumerable<Group>> GpSearchExploreGroups(int start = 0, int count = 20)
        {
            return await GpSearchGroups(AllGroupsUrl, start, count);
        }

        public static async Task<IEnumerable<Group>> GpSearchCultureGroups(int start = 0, int count = 20)
        {
            return await GpSearchGroups(CultureGroupsUrl, start, count);
        }

        public static async Task<IEnumerable<Group>> GpSearchTravelGroups(int start = 0, int count = 20)
        {
            return await GpSearchGroups(TravelGroupsUrl, start, count);
        }

        public static async Task<IEnumerable<Group>> GpSearchEntGroups(int start = 0, int count = 20)
        {
            return await GpSearchGroups(EntGroupsUrl, start, count);
        }

        public static async Task<IEnumerable<Group>> GpSearchFashionGroups(int start = 0, int count = 20)
        {
            return await GpSearchGroups(FashionGroupsUrl, start, count);
        }

        public static async Task<IEnumerable<Group>> GpSearchLifeGroups(int start = 0, int count = 20)
        {
            return await GpSearchGroups(LifeGroupsUrl, start, count);
        }

        public static async Task<IEnumerable<Group>> GpSearchTechGroups(int start = 0, int count = 20)
        {
            return await GpSearchGroups(TechGroupsUrl, start, count);
        }

        public static async Task<IEnumerable<Comment>> GpSearchTopicComments(string topicId, int start = 0, int count = 20)
        {
            var url = BuildUri(string.Format(TopicCommentsUrl, topicId));

            return JsonUtil.DeseriEnumerable<Comment>(await Search(url, bearer: false, start: start, count: count),
                "$.comments[*]");
        }


        private static async Task<IEnumerable<Topic>> GpSearchTopics(string url, int start = 0, int count = 20)
        {
            url = BuildUri(url);

            return JsonUtil.DeseriEnumerable<Topic>(await Search(url, bearer: false, start: start, count: count),
                "$.topics..topic");
        }

        private static async Task<IEnumerable<Group>> GpSearchGroups(string url, int start = 0, int count = 20)
        {
            url = BuildUri(url);

            return JsonUtil.DeseriEnumerable<Group>(await Search(url, bearer: false, start: start, count: count),
                "$.groups[*]");
        }

    
        internal static async Task<Authenticate> AuthGetToken(string userName, string password)
        {
            var json = await WebUtil.Post(AuthUrl, new[]
            {
                new KeyValuePair<string, string>("client_id", Common.API_KEY),
                new KeyValuePair<string, string>("redirect_uri", Common.REDIRECT_URI),
                new KeyValuePair<string, string>("grant_type", Common.GRANT_TYPE),
                new KeyValuePair<string, string>("client_secret", Common.CLIENT_SECRET),
                new KeyValuePair<string, string>("username", userName),
                new KeyValuePair<string, string>("password", password),
            });
            var authenticate = JsonUtil.Deserialize<Authenticate>(json);

            authenticate.GetTokenTime = DateTime.Now;
            return authenticate;
        }

        internal static async Task<Comment> GPAddComment(string topicId, string comment,string commentId = "")
        {
            var  url = BuildUri(AddComment, topicId);
            var json = await PostWithBearer(url, new[] {
                   new KeyValuePair<string, string>("comment_id", commentId),
                   new KeyValuePair<string, string>("content", comment),
            });

            return JsonUtil.Deserialize<Comment>(json);
        }
    }
}
